package com.company.zuoye;

import java.util.*;

/**
 * @author 关敬元
 * @Description
 * @date 2019/11/28
 */
public class Test {
    public static void main(String[] args) {
        ShoppingCart sp1 = new ShoppingCart("洗发水",30.0,2,60.0);
        ShoppingCart sp2 = new ShoppingCart("玩具车",50.0,1,50.0);
        ShoppingCart sp3 = new ShoppingCart("微波炉",83.5,1,83.5);
        ShoppingCart sp4 = new ShoppingCart("电饭锅",135.0,2,270.0);
        Map<Integer,ShoppingCart> map = new HashMap<Integer,ShoppingCart>();
        map.put(1,sp1);
        map.put(2,sp2);
        map.put(3,sp3);
        map.put(4,sp4);
        System.out.println("----------keyset遍历打印-----------");
        Set<Integer> keys = map.keySet();
        for (Integer obj:keys){
            ShoppingCart shoppingCart = map.get(obj);
            System.out.println("订单编号："+obj+"\t商品名称："+shoppingCart.getName()
                    +"\t商品总价："+shoppingCart.getSumprice()+"元");
        }
        System.out.println("-----------values遍历打印-----------------");
        Collection<ShoppingCart> values = map.values();
        for (ShoppingCart scart:values){
            System.out.println(scart);
        }
        System.out.println("----------entrySet遍历打印----------------");
        Set<Map.Entry<Integer,ShoppingCart>> set=map.entrySet();
        Iterator<Map.Entry<Integer,ShoppingCart>> iterator=set.iterator();
        while(iterator.hasNext()){
            Map.Entry<Integer,ShoppingCart> order=iterator.next();
            System.out.print("订单编号"+order.getKey());
            System.out.println("商品名称："+order.getValue().getName()
                    +"\t商品单价："+order.getValue().getPrice()+"元"
                    +"\t商品数量："+order.getValue().getNum()+"个"
                    +"\t商品总价："+order.getValue().getSumprice()+"元");
        }
    }
}
